import subprocess
import time
"""
将Python代码转换为C语言，并使用Python C API来编写扩展模块
"""

# 删除所有的pyd文件
import os

for i in os.listdir('.'):
    if i.endswith('.pyd'):
        os.remove(i)
        print(f'Deleted: {i}')

result = subprocess.run(
    [r'..\.venv\Scripts\python.exe', 'setup.py', 'build_ext', '--inplace'],
    capture_output=True,
    text=True
)
print(result.stdout)
print(result.stderr)

time.sleep(1)

# 删除所有后缀为c和html文件
for i in os.listdir('.'):
    if i.endswith('.c') or i.endswith('.html'):
        os.remove(i)
        print(f'Deleted: {i}')

# 移动文件当当前目前目录下
import shutil
target_dir = r'build\lib.win-amd64-cpython-312\expand_module'
for i in os.listdir(target_dir):
    shutil.move(os.path.join(target_dir, i), '.')
    print(f'Moved: {i}')

# 删除 build 文件夹
import shutil

if os.path.exists('build'):
    shutil.rmtree('build')
    print('Deleted: build file')
